package com.zpxt.zpxt.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zpxt.zpxt.entity.Resume;
import com.zpxt.zpxt.entity.Talent;
import com.zpxt.zpxt.serve.TalentServe;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author:hjx
 * @Description:
 * @Date: Created in 18:51 2018/4/16
 * @Modified By:
 */
@CrossOrigin
@RestController
@RequestMapping("/talent")
public class TalentController {
    @Resource(name="talentServe")
    TalentServe talentServe;
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    //  增简历
    @RequestMapping("/addResume")
    @ResponseBody
    public String addResume(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String info = request.getParameter("data");
        Resume resume=gson.fromJson(info, Resume.class);

        //添加简历信息
        talentServe.addResume(resume);

        //返回结果
        return gson.toJson("OK");
    }
    //  改简历
    @RequestMapping("/editResume")
    @ResponseBody
    public String editResume(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String info = request.getParameter("data");
        Resume resume=gson.fromJson(info, Resume.class);
        Long talentId = Long.parseLong(request.getParameter("talentId"));

        //查询简历信息
        talentServe.editResume(resume,talentId);

        //返回结果
        return gson.toJson("OK");
    }
    //  删除简历
    @RequestMapping("/delResume")
    @ResponseBody
    public String delResume(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        Long talentId = Long.parseLong(request.getParameter("talentId"));

        //删除简历信息
        talentServe.delResume(id,talentId);

        //返回结果
        return gson.toJson("OK");
    }
    //  查询简历列表
    @RequestMapping("/resumeList")
    @ResponseBody
    public List<Resume> findResumeList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long talentId = Long.parseLong(request.getParameter("talentId"));

        //查询简历信息
        List<Resume> resumes = talentServe.findResumeList(talentId);

        //返回结果
        return resumes;
    }
    //  查询简历
    @RequestMapping("/findResumeOne")
    @ResponseBody
    public Resume findResumeOne(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));

        //查询简历信息
        Resume resume = talentServe.findResumeOne(id);

        //返回结果
        return resume;
    }
    // 申请岗位
    @RequestMapping("/apply")
    @ResponseBody
    public String apply(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long positionId = Long.parseLong(request.getParameter("positionId"));
        Long resumeId = Long.parseLong(request.getParameter("resumeId"));

        talentServe.apply(positionId,resumeId);

        return gson.toJson("OK");
    }
// 申请列表
    @RequestMapping("/appList")
    @ResponseBody
    public List<Map> appList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long positionId = Long.parseLong(request.getParameter("talentId"));

        List<Map> list = talentServe.appList(positionId);

        return list;
    }
// 删除申请
    @RequestMapping("/delApp")
    @ResponseBody
    public String delApp(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        Long resumeId = Long.parseLong(request.getParameter("resumeId"));

        talentServe.delApp(id,resumeId);

        return gson.toJson("OK");
    }
    //   查询求职者信息
    @RequestMapping("/findTalentInfo")
    @ResponseBody
    public Talent findTalentInfo(HttpServletRequest request, HttpServletResponse response){
        Long talentId = Long.parseLong(request.getParameter("talentId"));
        Talent talent = talentServe.findTalentInfo(talentId);
        return talent;
    }
}
